from functools import wraps
import time
import logging

def timeuse(fuc):
    @wraps(fuc)
    def mywarp(*args, **kwargs):
        start_time = time.time()
        result = fuc(*args, **kwargs)
        end_time = time.time()
        print(fuc.__name__, end_time - start_time)
        return result

    return mywarp

@timeuse
def count(n):
    while (n):
        n -= 1
    return "haha,finished"


#带参数的装饰器
def mylog(level, name=None, message=None):

    def decorate(func):
        logname=name if name else func.__name__
        log = logging.getLogger(logname)
        logmsg = message if message else func.__name__

        @wraps(func)
        def mywrapper(*args, **kwargs):
            log.log(level, logmsg)
            return func(*args, **kwargs)

        return mywrapper

    return decorate

@mylog(logging.DEBUG)
def add(a, b):
    return a+b

if '__main__' == __name__:
    # print(count(10000000))
    add(1, 2)